package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lnking.lnkingplace.Util.LocalDateTimeUtil;
import com.lnking.lnkingplace.mapper.IsolationAfterPositivePersonnelMapper;
import com.lnking.lnkingplace.mapper.IsolationPlaceMapper;
import com.lnking.lnkingplace.model.dto.isolation.afterPositivePersonnel.AfterPositivePersonnelImport;
import com.lnking.lnkingplace.model.entity.IsolationAfterPositivePersonnel;
import com.lnking.lnkingplace.model.enums.DateFormat;
import com.lnking.lnkingplace.model.request.isolation.afterPositivePersonnel.AfterPositivePersonnelAddRequest;
import com.lnking.lnkingplace.model.request.isolation.afterPositivePersonnel.AfterPositivePersonnelUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationAfterPositivePersonnelService;
import com.lnking.starter.export.utils.ImportOption;
import com.lnking.starter.export.utils.ImportUtils;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.xml.transform.sax.SAXTransformerFactory;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangqt
 * @since 2022-09-21
 */
@Service
public class IsolationAfterPositivePersonnelServiceImpl extends CustomServiceImpl<IsolationAfterPositivePersonnelMapper, IsolationAfterPositivePersonnel> implements IIsolationAfterPositivePersonnelService {

    @Resource
    IsolationPlaceMapper isolationPlaceMapper;

    @Override
    public void importExcel(MultipartFile file)throws Exception {
        ImportOption option = new ImportOption.Builder().build();
        List<AfterPositivePersonnelImport> result =  ImportUtils.importExcel(file, AfterPositivePersonnelImport.class,option);
        result.forEach(r ->{
            IsolationAfterPositivePersonnel info = new IsolationAfterPositivePersonnel();
            BeanUtils.copyProperties(r,info);
            info.setCreateTime(LocalDateTime.now());
            info.setAge(Integer.parseInt(r.getAge()));
            //查询placeId
            String placeId = isolationPlaceMapper.getPlaceId(r.getPlaceName());
            Assert.isTrue(StringUtils.isNotBlank(placeId), "该场所不存在："+"【"+r.getPlaceName()+"】");
            info.setPlaceId(placeId);
            if(StringUtils.isNotBlank(r.getStartDate())){
                LocalDateTime startTime = LocalDateTimeUtil.stringToLocalDateTime(r.getStartDate(), DateFormat.LOCAL_DATE_TIME);
                info.setCreateTime(startTime);
            }
            this.baseMapper.insert(info);
        });
    }

    @Override
    public void add(AfterPositivePersonnelAddRequest params) {
        IsolationAfterPositivePersonnel info = new IsolationAfterPositivePersonnel();
        BeanUtils.copyProperties(params,info);
        info.setCreateTime(LocalDateTime.now());
        String placeId = isolationPlaceMapper.getPlaceId(params.getPlaceName());
        info.setPlaceId(placeId);
        this.baseMapper.insert(info);
    }

    @Override
    public void update(AfterPositivePersonnelUpdateRequest params) {
        IsolationAfterPositivePersonnel info = new IsolationAfterPositivePersonnel();
        BeanUtils.copyProperties(params,info);
        String placeId = isolationPlaceMapper.getPlaceId(params.getPlaceName());
        info.setPlaceId(placeId);
        this.baseMapper.updateById(info);
    }
}
